
using System;
using System.Collections;
using System.Web.UI.WebControls;

namespace GiftNet.Model
{
    #region Address Model -(by fangyifeng at 2007-09-14 12:23:52)

    /// <summary>
    /// Address object'Address'.
    /// </summary>
    [Serializable]
    public class Address : System.IComparable
    {
        #region Member Variables

        protected Guid _id;
        protected string _street;
        protected string _postcode;
        protected District _district;

        protected static String _sortExpression = "Id";
        protected static SortDirection _sortDirection = SortDirection.Ascending;

        #endregion

        #region Constructors

        public Address() { }

        public Address(string street, string postcode, District district)
        {
            this._street = street;
            this._postcode = postcode;
            this._district = district;
        }

        #endregion

        #region Public Properties

        public virtual Guid Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public virtual string Street
        {
            get { return _street; }
            set
            {
                if (value != null && value.Length > 200)
                    throw new ArgumentOutOfRangeException("Invalid value for Street", value, value.ToString());
                _street = value;
            }
        }

        public virtual string Postcode
        {
            get { return _postcode; }
            set
            {
                if (value != null && value.Length > 30)
                    throw new ArgumentOutOfRangeException("Invalid value for Postcode", value, value.ToString());
                _postcode = value;
            }
        }

        public virtual District District
        {
            get { return _district; }
            set { _district = value; }
        }


        public static String SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

        public static SortDirection SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }
        #endregion

        #region IComparable Methods
        public virtual int CompareTo(object obj)
        {
            if (!(obj is Address))
                throw new InvalidCastException("This object is not of type Address");

            int relativeValue;
            switch (SortExpression)
            {
                case "Id":
                    relativeValue = this.Id.CompareTo(((Address)obj).Id);
                    break;
                case "Street":
                    relativeValue = this.Street.CompareTo(((Address)obj).Street);
                    break;
                case "Postcode":
                    relativeValue = (this.Postcode != null) ? this.Postcode.CompareTo(((Address)obj).Postcode) : -1;
                    break;
                default:
                    goto case "Id";
            }
            if (Address.SortDirection == SortDirection.Ascending)
                relativeValue *= -1;
            return relativeValue;
        }
        #endregion
    }

    #endregion
}
